package com.magnificent.atlantis.server.database.message;

import com.magnificent.atlantis.common.entity.ServerMeeting;
import com.magnificent.atlantis.common.entity.messages.ServerInviteMessage;
import com.magnificent.atlantis.common.entity.messages.ServerMessage;
import com.magnificent.atlantis.common.entity.messages.ServerNewMeetingMessage;
import com.magnificent.atlantis.server.database.AtlantisDataSource;
import com.magnificent.commons.db.datasource.ObjectEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Babanin
 * Date: 06.05.2009
 * Time: 12:43:56
 */
public class NewMeetingMessageHandler extends MessageHandler {
    public ObjectEntity handleMessage(ServerMessage message) {
        ServerNewMeetingMessage newMeetingMessage = (ServerNewMeetingMessage) message;
        ServerMeeting serverMeeting = generateServerMeeting(newMeetingMessage);

        AtlantisDataSource.getInstance().persist(serverMeeting);

        // Send invitations
        List<Integer> invitedUsers = newMeetingMessage.getInvitedUsers();
        for (Integer invitedUser : invitedUsers) {
            if (newMeetingMessage.getUserFromId() != invitedUser) {
                AtlantisDataSource.getInstance().persist(generateInvite(serverMeeting, invitedUser));
            }
        }

        return serverMeeting;
    }

    private ServerInviteMessage generateInvite(ServerMeeting serverMeeting, Integer invitedUser) {
        ServerInviteMessage inviteMessage = new ServerInviteMessage();

        inviteMessage.setSentDate(new Date());
        inviteMessage.setUserToId(invitedUser);
        inviteMessage.setDate(serverMeeting.getDate());
        inviteMessage.setTitle(serverMeeting.getTitle());
        inviteMessage.setEventId((int) serverMeeting.getId());
        inviteMessage.setComment(serverMeeting.getDescription());
        inviteMessage.setInvited(serverMeeting.getInvitedUsers());
        inviteMessage.setUserFromId(serverMeeting.getModeratorId());

        return inviteMessage;
    }

    private ServerMeeting generateServerMeeting(ServerNewMeetingMessage meetingMessage) {
        ServerMeeting serverMeeting = new ServerMeeting();

        ArrayList<Integer> invited = meetingMessage.getInvitedUsers();
        if (!invited.contains(meetingMessage.getUserFromId())) {
            invited.add(meetingMessage.getUserFromId());
        }
        ArrayList<Integer> accepted = new ArrayList<Integer>();
        accepted.add(meetingMessage.getUserFromId());


        serverMeeting.setAcceptedByUsers(accepted);
        serverMeeting.setDate(meetingMessage.getDate());
        serverMeeting.setTitle(meetingMessage.getTitle());
        serverMeeting.setDescription(meetingMessage.getMessage());
        serverMeeting.setInvitedUsers(meetingMessage.getInvitedUsers());
        serverMeeting.setCanceledByUsers(new ArrayList<Integer>());
        serverMeeting.setModeratorId(meetingMessage.getUserFromId());

        return serverMeeting;
    }
}
